package common;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

public class HttpUtils {

    public static HashMap<String,String> sendGetRequest(String url) throws IOException {
        HashMap resultMap= new HashMap<>();
        URL requestUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
        connection.setRequestMethod("GET");
        int responseCode = connection.getResponseCode();
        InputStream inputStream=responseCode==HttpURLConnection.HTTP_OK?connection.getInputStream():connection.getErrorStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder response = new StringBuilder();
        String line;

        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        resultMap.put("code",String.valueOf(responseCode));
        resultMap.put("result",response);
        System.out.println("GET request, response code: " + responseCode+" ,result:"+response);
        return resultMap;
    }

    public static HashMap<String,String> sendPutRequest(String url, String data) throws IOException {
        HashMap resultMap= new HashMap<>();
        URL requestUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
        connection.setRequestMethod("PUT");
        connection.setDoOutput(true);

        try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
            outputStream.writeBytes(data);
            outputStream.flush();
        }

        int responseCode = connection.getResponseCode();
        InputStream inputStream=
                responseCode==HttpURLConnection.HTTP_OK
                        ||responseCode==HttpURLConnection.HTTP_CREATED
                        ||responseCode==HttpURLConnection.HTTP_NO_CONTENT
                ?connection.getInputStream():connection.getErrorStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder response = new StringBuilder();
        String line;

        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        resultMap.put("code",String.valueOf(responseCode));
        resultMap.put("result",response);
        System.out.println("PUT request, response code: " + responseCode+" ,result:"+response);
        return resultMap;
    }

    public static HashMap<String,String> sendPostRequest(String url, String data) throws IOException {
        HashMap resultMap= new HashMap<>();
        URL requestUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);

        try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
            outputStream.writeBytes(data);
            outputStream.flush();
        }

        int responseCode = connection.getResponseCode();
        InputStream inputStream=responseCode==HttpURLConnection.HTTP_OK?connection.getInputStream():connection.getErrorStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder response = new StringBuilder();
        String line;

        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        resultMap.put("code",String.valueOf(responseCode));
        resultMap.put("result",response);
        System.out.println("POST request, response code: " + responseCode+" ,result:"+response);
        return resultMap;
    }
}
